package com.hjxy.controller;

import com.github.pagehelper.PageInfo;
import com.hjxy.pojo.Course;
import com.hjxy.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName:
 * @description:
 * @author: yh
 * @createDate: 2020/9/29
 * @version: 1.0
 */
@Controller
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @RequestMapping("/getcoursepage")
    public String coursepage(){
        return "course";
    }

//获取课程的全部信息
    @RequestMapping("/getcourse")
    @ResponseBody
    public Map<String,Object> getcourse(){
        List<Course> courseList=courseService.geyAll();
     //   System.out.println(courseList);
        Map<String,Object> result=new HashMap<>();
        result.put("code",0);
        result.put("message","获取成功");
        result.put("courseList",courseList);
      //  System.out.println(courseList);
       return result;
    }
    /*
   Integer和int的区别：
   int是数据类型，没有空值（null）
   Integer是对象， 有空值(null)
   18216754601
    */
    //分页
    @RequestMapping("/getCourseByPage")
    @ResponseBody
    public Map<String, Object> getCourseByBage(Integer currentPage,Integer pageSize){
        PageInfo<Course> pageinfo =courseService.getCoursePage(currentPage,pageSize);
        System.out.println(pageinfo);
        Map<String,Object> result=new HashMap<>();
        result.put("code",0);
        result.put("message","获取成功");
        result.put("courseList",pageinfo.getList());//返回查询出来的分页数据
        result.put("totalPage",pageinfo.getPages());//返回当前总共有多少页
   return result;
    }

    /*加载的课程信息（用于编辑一个课程信息）*/
    @RequestMapping("/getone")
    @ResponseBody
    public Map<String, Object> getone(Integer courseId){
       Course course= courseService.getone(courseId);
        System.out.println("_____________");
        System.out.println(course);
        Map<String,Object> result=new HashMap<>();
       if(course==null){
           result.put("code",-1);
           result.put("message","获取失败");
       }else {
           result.put("code",0);
           result.put("message","获取成功");
           result.put("course",course);
       }
        return result;
    }
    @RequestMapping("/eidt")
    @ResponseBody
    public Map<String,Object> edit(Course course){
        Map<String,Object> result=new HashMap<>();
       boolean modified= courseService.modifyone(course);
        if(!modified){
            result.put("code",-1);
            result.put("message","编辑失败");
        }else {
            result.put("code",0);
            result.put("message","编辑成功");
        }
        return result;
    }
    //逻辑删除
    @RequestMapping("/getdelete1")
    @ResponseBody
    public Map<String,Object> delete1(Course course){
        Map<String,Object> result=new HashMap<>();
       boolean deletelj= courseService.deletelj(course);
        if(!deletelj){
            result.put("code",-1);
            result.put("message","删除失败");
        }else {
            result.put("code",0);
            result.put("message","删除成功");
        }
        return result;
    }
    @RequestMapping("/addPage")
    @ResponseBody
    public Map<String,Object> add(Course course){
        Map<String,Object> result=new HashMap<>();
        boolean addClass= courseService.addClass(course);
        if(!addClass){
            result.put("code",-1);
            result.put("message","添加失败");
        }else {
            result.put("code",0);
            result.put("message","添加成功");
        }
        return result;
    }
    @RequestMapping("/search")
    @ResponseBody
    public Map<String,Object> search(String search){
        Map<String,Object> result=new HashMap<>();
       List<Course> courseList= courseService.searchAll(search);
        if(courseList !=null){
            result.put("code",0);
            result.put("message","添加成功");
            result.put("courseList",courseList);

        }else {
            result.put("code",-1);
            result.put("message","添加失败");
        }
        return result;
    }
}
